package com.blackcat.v2.demo;

public class MyLifecycleSupport {

    /**
     * 被管理的lifecycle实例
     */
    private MyLifecycle lifecycle ;

    private MyLifecycleListener[] listeners ;

    public MyLifecycleSupport(MyLifecycle lifecycle) {
        this.lifecycle = lifecycle;
    }

    public void addLifecycleListener(MyLifecycleListener listener){
        if(listeners == null){
            listeners = new MyLifecycleListener[1];
            listeners[0] = listener ;
        }else{
            MyLifecycleListener[] newListeners = new MyLifecycleListener[listeners.length + 1];
            newListeners[newListeners.length - 1] = listener ;
            listeners = newListeners ;
        }

    }

    public MyLifecycleListener[] getLifecycleEvents(){
        return listeners ;
    }

    /**
     * 触发所有的监听器
     * @param type  事件类型
     * @param data
     */
    public void fireLifecycleEvent(String type , Object data){
        if(listeners != null && listeners.length > 0){
            for (MyLifecycleListener lifecycleListener : listeners) {
                lifecycleListener.lifecycleEvent(new MyLifecycleEvent(lifecycle, type, data));
            }
        }
    }

}
